package com.example.news.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.example.news.R;
import com.example.news.News.NewsAdapter;
import com.example.news.News.News;
import com.example.news.News.NewsList;
import com.example.news.News.TianxinManager;
import com.example.news.Video.MyLayoutManager;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class NewsFragment extends Fragment {

    private String title;
    private String source;
    private String time;
    private String picurl;
    private String html;

    private List<News> list = new ArrayList<>();
    public RecyclerView recyclerView;
    private NewsAdapter contAdapter;
    private View view;
    OkHttpClient okHttpClient = new OkHttpClient();



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.newsfragment,container,false);

        iniINT();
        iniRec();
        return view;
    }

    private void iniRec(){

        recyclerView = view.findViewById(R.id.rv_main_list);
        contAdapter = new NewsAdapter(getActivity(),list);
        recyclerView.setAdapter(contAdapter);
        recyclerView.setLayoutManager(new MyLayoutManager(getActivity(), OrientationHelper.VERTICAL, false));
        //recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
//        list.add(new News("穿lo裙在旅顺博物馆门口跳宅舞 网红党妹道歉",
//                "中华社会",
//                "2021-07-04 00:00",
//                "https://img0.utuku.imgcdc.com/300x200/news/20210704/187bca59-5d3a-450f-bc9e-841eb99cf344.jpg",
//                "https://news.china.com/social/1007/2021070/39721220.html"));
//        contAdapter.notifyDataSetChanged();
    }

    //网络获取数组
    private void iniINT(){

        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .url(TianxinManager.url)
                        .build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String result = response.body().string();
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Gson gson = new Gson();
                                NewsList newsList = gson.fromJson(result, NewsList.class);
                                for(int i=0; i < 30; i++) {
                                   title = newsList.getResult().getResult().getList().get(i).getTitle();
                                   source = newsList.getResult().getResult().getList().get(i).getSrc();
                                   time = newsList.getResult().getResult().getList().get(i).getTime();
                                   picurl = newsList.getResult().getResult().getList().get(i).getPic();
                                   html = newsList.getResult().getResult().getList().get(i).getContent();
                                    list.add(new News(title, source, time, picurl,html));
                                }

                                contAdapter.notifyDataSetChanged();
                            }
                        });

                    }
                });

            }
        }).start();

    }




    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.e("TAG", "OneFragment onHiddenChanged :" + hidden);
    }
}
